﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using SupplyChain.Domain.Entities;
using SupplyChain.ErrorCode;
using SupplyChain.Write.API.Application.Command.CustomerOrder;

namespace SupplyChain.Write.API.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class CustomerOrderController : ControllerBase
    {
        private readonly IMediator mediator;

        public CustomerOrderController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 获取销售订单列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResponse<List<CustomerOrder>>> GetAllCOList()
        {
            return ApiResponse<List<CustomerOrder>>.Success(await mediator.Send(new GetCustomerOrderCommand()));
        }

        /// <summary>
        /// 创建销售订单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResponse<bool>> AddCustomerOrder(AddCustomerOrderCommand command)
        {
            bool result = await mediator.Send(command);
            if (result)
            {
                return ApiResponse<bool>.Success(result);
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }

        /// <summary>
        /// 修改销售订单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResponse<bool>> UpdateCustomerOrder(UpdCustomerOrderCommand command)
        {
            bool result = await mediator.Send(command);
            if (result)
            {
                return ApiResponse<bool>.Success(result);
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }

        /// <summary>
        /// 删除销售订单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResponse<bool>> DeleteCustomerOrder([FromQuery] DelCustomerOrderCommand command)
        {
            bool result = await mediator.Send(command);
            if (result)
            {
                return ApiResponse<bool>.Success(result);
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }

        /// <summary>
        /// 获取销售合同类型列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResponse<List<ContractCategory>>> GetContractCategory()
        {
            return ApiResponse<List<ContractCategory>>.Success(await mediator.Send(new GetContractCategoryCommand()));
        }

        /// <summary>
        /// 新增销售合同类型
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResponse<bool>> AddContractCategory(AddContractCategoryCommand command)
        {
            bool result = await mediator.Send(command);
            if (result)
            {
                return ApiResponse<bool>.Success(result);
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }

        /// <summary>
        /// 修改销售合同类型
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResponse<bool>> UpdateContractCategory(UpdContractCategoryCommand command)
        {
            bool result = await mediator.Send(command);
            if (result)
            {
                return ApiResponse<bool>.Success(result);
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }

        /// <summary>
        /// 删除销售合同类型
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResponse<bool>> DeleteContractCategory([FromQuery] DelContractCategoryCommand command)
        {
            bool result = await mediator.Send(command);
            if (result)
            {
                return ApiResponse<bool>.Success(result);
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }
    }
}
